import os
import shutil
from Utils import *
file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(file_path).replace("\\file","")
srcAssets = dir_path +'\\mipmap-xhdpi'
desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\res\\mipmap-xhdpi'
print('资源icon路径::\n'+srcAssets)
print('替换icon路径::\n'+desAssets)

copyFiles(srcAssets, desAssets)
resAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\res'
if(os.path.exists(resAssets + '\\mipmap-hdpi')):
    deleteDir(resAssets + '\\mipmap-hdpi')
if(os.path.exists(resAssets + '\\mipmap-mdpi')):
    deleteDir(resAssets + '\\mipmap-mdpi')
if(os.path.exists(resAssets + '\\mipmap-xxhdpi')):
    deleteDir(resAssets + '\\mipmap-xxhdpi')
shutil.rmtree(dir_path+'\\file\\__pycache__')


srcAssets = dir_path +'\\file\\AppActivity.java'
desAssets = dir_path.replace("tools","")+'build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\app\src\org\cocos2dx\javascript\\AppActivity.java'

with open(srcAssets, "r", encoding="utf-8") as file:
    tex_read = file.read()
with open(desAssets, "w") as file:
    file.write(tex_read)

with open(dir_path.replace("\\tools","\\settings\\builder.json"), 'r') as file:
    jsonData = json.load(file)
tex_read = tex_read.replace("import org.cocos2d.demo","import "+jsonData['android']['packageName'])
with open(desAssets, "w") as file:
    file.write(tex_read)
print('javascript路径::\n'+desAssets)


#替换游戏名称
xml_path = get_files_xml_list(dir_path,'.xml')[0]
with open(xml_path, "r", encoding="utf-8") as file:
    tex_read = file.read()
with open(dir_path.replace("\\tools","\\build\\jsb-default\\frameworks\\runtime-src\\proj.android-studio\\res\\values\\strings.xml"), "w", encoding="utf-8") as file:
    file.write(tex_read)


print('完成任务')

# E:\Siki\Cocos\tvBox_cc\build\jsb-default\frameworks\runtime-src\proj.android-studio\res
# E:\Siki\Cocos\tvBox_cc\build\jsb-default\frameworks\runtime-src\proj.android-studio\app\src\org\cocos2dx\javascript